package cn.hahah.module.blog.service.sysnotice;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.sysnotice.vo.*;
import cn.hahah.module.blog.dal.dataobject.sysnotice.SysNoticeDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.sysnotice.SysNoticeMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 博客系统公告 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SysNoticeServiceImpl implements SysNoticeService {

    @Resource
    private SysNoticeMapper sysNoticeMapper;

    @Override
    public Long createSysNotice(SysNoticeSaveReqVO createReqVO) {
        // 插入
        SysNoticeDO sysNotice = BeanUtils.toBean(createReqVO, SysNoticeDO.class);
        sysNoticeMapper.insert(sysNotice);
        // 返回
        return sysNotice.getId();
    }

    @Override
    public void updateSysNotice(SysNoticeSaveReqVO updateReqVO) {
        // 校验存在
        validateSysNoticeExists(updateReqVO.getId());
        // 更新
        SysNoticeDO updateObj = BeanUtils.toBean(updateReqVO, SysNoticeDO.class);
        sysNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSysNotice(Long id) {
        // 校验存在
        validateSysNoticeExists(id);
        // 删除
        sysNoticeMapper.deleteById(id);
    }

    private void validateSysNoticeExists(Long id) {
        if (sysNoticeMapper.selectById(id) == null) {
            throw exception(SYS_NOTICE_NOT_EXISTS);
        }
    }

    @Override
    public SysNoticeDO getSysNotice(Long id) {
        return sysNoticeMapper.selectById(id);
    }

    @Override
    public PageResult<SysNoticeDO> getSysNoticePage(SysNoticePageReqVO pageReqVO) {
        return sysNoticeMapper.selectPage(pageReqVO);
    }

}